package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.model.ForgeItemList;
import labox.innovation.gameserver.model.L2ItemInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.serverpackets.ForgeItemSucess;
import labox.innovation.gameserver.templates.item.ItemStarManger;

/**
 * 客户端请求 物品追星
 * @author zjj
 *
 */
public class RequestStarItem extends L2GameClientPacket {

	private static final String RequestStarItem = "[C] 0x7c RequestStarItem";
	
	private static final Logger _log = LoggerFactory.getLogger(RequestStarItem.class.getName());

	//	private long _chanceId; //影响追星成功率的道具 uid
	//
	//	private long _keepId; //保持追星不掉等级的道具 uid

	private long _itemId; //追星物品的uid

	@Override
	public String getType() {
		return RequestStarItem;
	}

	@Override
	protected void readImpl() {
		//		_itemId = readLong();
		//		_chanceId = readLong();
		//		_keepId = readLong();

	}

	@Override
	protected void runImpl() {

		FuzePcInstance player = getClient().getActiveChar();
		if (player == null) {
			return;
		}

		ForgeItemList forgeList = player.getActiveForgeItemList();
		if (forgeList == null) {
			return;
		}

		if (forgeList.getForgeType() != RequestForgeItem.OP_START) {
			return;
		}

		_itemId = forgeList.getMainItemId();

		L2ItemInstance item = player.getInventory().getItemByObjectId(_itemId);
		if (item == null) {
			return;
		}
		
		if(_log.isDebugEnabled())
			_log.debug("{} 请求追星",player);


		int res = ItemStarManger.getInstance().statItem(getClient().getActiveChar(), _itemId, forgeList.getChanceItemId(), forgeList.getKeepItemId());
		if (res == ItemStarManger.STAR_RES_FAILED) {
			//TODO:
			//失败  。  系统消息
			//			player.sendPacket( new SystemMessage( SystemMessageId.ITEM_FORGE_FAILED ));
			//发送 成功消息给客户端
			player.sendPacket(new ForgeItemSucess(player, item, RequestForgeItem.OP_START, 0));
			
			if(_log.isDebugEnabled())
				_log.debug("追星失败!");
			
			player.getActiveForgeItemList().clear();
		} else if (res == ItemStarManger.STAR_RES_SUCESS) {
			//发送 成功消息给客户端
			player.sendPacket(new ForgeItemSucess(player, item, RequestForgeItem.OP_START, 1));
			player.sendMessage("追星成功，品质上升");
			player.getActiveForgeItemList().clear();

		}

	}

}
